package ma.platform.server.student.controller;

import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import ma.plaform.server.common.log.annotation.SysLog;
import ma.platform.server.core.exception.CheckedException;
import ma.platform.server.core.util.R;
import ma.platform.server.student.entity.TbParent;
import ma.platform.server.student.entity.TbParent;
import ma.platform.server.student.entity.TbStudent;
import ma.platform.server.student.service.TbParentService;
import ma.platform.server.student.service.TbParentService;
import ma.platform.server.student.service.TbStudentService;
import org.apache.commons.collections.CollectionUtils;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/parent")
@Tag(description = "parent", name = "家长信息管理模块")
public class ParentController {

    @Resource
    private TbParentService tbParentService;

    @Resource
    private TbStudentService studentService;

    @GetMapping("/page")
    public R page(@ParameterObject Page page, @ParameterObject TbParent parent) {
        Page<TbParent> resultPage = tbParentService.page(page, Wrappers.<TbParent>lambdaQuery()
                .like(StrUtil.isNotBlank(parent.getNickName()), TbParent::getNickName, parent.getNickName())
                .eq(Objects.nonNull(parent.getPhone()), TbParent::getPhone, parent.getPhone()));
        List<TbParent> records = resultPage.getRecords();
        if (CollectionUtils.isNotEmpty(records)){
            for (TbParent record : records) {
                record.setPhone(DesensitizedUtil.mobilePhone(record.getPhone()));
            }
        }
        return R.ok(resultPage);
    }

    @GetMapping("/list")
    public R list(@ParameterObject TbParent parent) {
        return R.ok(tbParentService.list(Wrappers.<TbParent>lambdaQuery()
                .like(StrUtil.isNotBlank(parent.getNickName()), TbParent::getNickName, parent.getNickName())
                .eq(Objects.nonNull(parent.getPhone()), TbParent::getPhone, parent.getPhone())));
    }

    /**
     * 添加学生
     *
     * @return success、false
     */
    @SysLog("添加家长信息")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('tb_parent_add')")
    public R save(@Valid @RequestBody TbParent parent) {
        tbParentService.save(parent);
        return R.ok(parent);
    }

    /**
     * 删除学生
     *
     * @param ids ID
     * @return R
     */
    @SysLog("删除家长信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('tb_parent_del')")
    public R removeById(@RequestBody Long[] ids) {
        List<TbStudent> parentList = studentService.list(Wrappers.<TbStudent>lambdaQuery().in(TbStudent::getParentId, ids));
        if (CollectionUtils.isNotEmpty(parentList)){
            throw new CheckedException("该家长信息下有学生，无法删除");
        }
        return R.ok(tbParentService.removeByIds(Lists.newArrayList(ids)));
    }

    /**
     * 修改学生
     *
     * @return success/false
     */
    @PutMapping
    @SysLog("修改家长信息")
    @PreAuthorize("@pms.hasPermission('tb_parent_edit')")
    public R updateById(@Valid @RequestBody TbParent parent) {
        tbParentService.updateById(parent);
        return R.ok();
    }

    /**
     * 通过ID查询信息
     * @param id ID
     */
    @GetMapping("/details/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(tbParentService.getById(id));
    }
}
